package com.softer.wxzj.common.util;
/**
 * @author Gong Zhiyang
 * @date 2019/5/7
 */

import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.entity.BaseModifyDetail;
import com.softer.wxzj.entity.BaseModifyReq;
import com.softer.wxzj.entity.BaseOwnerInfo;
import com.softer.wxzj.mapper.BaseModifyDetailMapper;
import com.softer.wxzj.mapper.BaseModifyReqMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2019/5/7
 */
@Component
public class ModifyUtilList<T> {
    @Autowired
    private BaseModifyDetailMapper modifyDetailMapper;
    @Autowired
    private BaseModifyDetailMapper baseModifyDetailMapper;
    @Autowired
    private BaseModifyReqMapper baseModifyReqMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;


    /**
     * 封装变更查询参数
     *
     * @param id 业务ID
     * @return map
     */
    public Map<String, Object> getMap(String id) {
        List<String> list = modifyDetailMapper.getColumnList(id);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", list);
        map.put("id", id);
        return map;
    }

    /**
     * @param oldObject
     * @param newObject
     * @param businessId  （业务id） 业主id
     * @param modifyType
     * @param modifyCause
     * @param uuid        reqid
     * @param id          变更数据的id
     * @param infoType
     * @return
     * @throws IllegalAccessException
     */
    public int updateToSave(T oldObject, T newObject, String businessId, String modifyType, String modifyCause, String uuid, String id, BaseOwnerInfo infoType)
            throws IllegalAccessException {
        int num = NumberEnum.ZERO.getNum();
        int count = flowInstanceMapper.getCountByBusinessId(businessId);
        if (0 == count) {
            num = saveModifyDetail(uuid, oldObject, newObject, id, infoType);
            if (num == 0) {
                return 0;
            }
            try {
                num = saveModifyReq(uuid, businessId, modifyType, modifyCause);
            } catch (Exception e) {
                throw e;
            }

        }
        //0没有要变更的数据,1添加中间表失败，2添加变更表失败，3添加变更表成功
        return num;
    }

    /**
     * 保存ModifyReq
     *
     * @param businessId
     * @param modifyType
     * @param modifyCause
     * @return
     */
    public int saveModifyReq(String uuid, String businessId, String modifyType, String modifyCause) throws RuntimeException {
        int num;
        try {
            num = baseModifyReqMapper.selectCountId(uuid);
            if (num != 0) {
                return num;
            }
        } catch (Exception e) {
            throw e;
        }
        BaseModifyReq baseModifyReq = new BaseModifyReq();
        baseModifyReq.setModifyType(modifyType);
        baseModifyReq.setModifyCause(modifyCause);
        baseModifyReq.setCreater(ShiroUserUtil.getUserName());
        baseModifyReq.setBusinessId(businessId);
        baseModifyReq.setCreater(ShiroUserUtil.getUserName());
        baseModifyReq.setId(uuid);
        return baseModifyReqMapper.insert(baseModifyReq);
    }

    /**
     * 保存变更中间表
     *
     * @param oldObject 新对象
     * @param newObject 旧对象
     * @param uuid      uuid
     * @throws IllegalArgumentException
     */
    int saveModifyDetail(String uuid, T oldObject, T newObject, String id, BaseOwnerInfo infoType) throws IllegalAccessException {
        int num = NumberEnum.ZERO.getNum();
        List<BaseModifyDetail> list = new ArrayList<>();
        list = new GetDifferenceUtil().getOnlyDifference(oldObject, newObject, infoType);
        if (list.size() == 0) {
            return NumberEnum.ZERO.getNum();
        }

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setCreater(ShiroUserUtil.getUserName());
            list.get(i).setReqNo(uuid);
            list.get(i).setBusinessId(id);
            list.get(i).setId(UUIDUtil.getUUID());
            num = baseModifyDetailMapper.insert(list.get(i));
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "变更失败，请重新操作");
            }
        }
        return num;
    }

}
